<template>
  <div class="newbody">
    <div class="newbody-form">
      <div class="size16 sizefw title">{{ t('changetransactionpassword') }}</div>
      <el-form ref="refruleForm" :model="ruleForm" :rules="rules" class="refruleForm">
        <el-form-item>
          <el-input size="large" v-model="userInfo.mobile" disabled>
            <template #append>
              <div @click.prevent.stop="sendSms" class="GetCode">{{ Timetext }}</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input size="large" :placeholder="t('pleaseentercode')" v-model="ruleForm.code">
            <template #append>
              <div>
                <graphicCode :identifyCode="identifyCode"></graphicCode>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="new_pass">
          <el-input
            size="large"
            :placeholder="t('pleaseenternewpassword')"
            v-model="ruleForm.new_pass"
          />
        </el-form-item>
        <el-form-item prop="new_pass_cp">
          <el-input
            size="large"
            :placeholder="t('pleaseenternewpassword')"
            v-model="ruleForm.new_pass_cp"
          />
        </el-form-item>
        <el-button
          v-loading="loading"
          :disabled="loading"
          class="btn btnbg"
          @click="submitForm(refruleForm)"
          size="large"
          >{{ t('confirm') }}</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'
const store = useStore() //vuex
import { reactive, ref, getCurrentInstance, computed } from 'vue'
const ruleForm = reactive({
  new_pass: '',
  new_pass_cp: '',
  code: '',
})
const refruleForm = ref() //表单验证
const loading = ref(false) //表单验证
const new_pass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error(t('pleaseenternewpassword')))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!refruleForm.value) return
      refruleForm.value.validateField('new_pass_cp', () => null)
    }
    callback()
  }
}
const new_pass_cp = (rule, value, callback) => {
  if (value === '') {
    callback(new Error(t('pleaseenternewpassword')))
  } else if (value !== ruleForm.new_pass) {
    callback(new Error(t('twopasswordsnotmatch')))
  } else {
    callback()
  }
}

const rules = reactive({
  new_pass: [
    {
      validator: new_pass,
      trigger: 'blur',
    },
  ],
  new_pass_cp: [
    {
      validator: new_pass_cp,
      trigger: 'blur',
    },
  ],
  code: [
    {
      required: true,
      message: t('pleaseentercode'),
      trigger: 'blur',
    },
  ],
})
const { proxy } = getCurrentInstance()
// 验证码
// 图形验证码
import graphicCode from '@/components/graphicCode.vue'
let identifyCode = ref('') //验证码
const userInfo = computed(() => {
  return store.state.userInfo
})
// 发送
const TimeState = ref(true)
const sendSms = () => {
  if (!TimeState.value) return
  TimeState.value = false
  proxy
    .$post('/mt4/Login/sendSms', { mobile: userInfo.value.mobile })
    .then((res) => {
      ElMessage({
        message: res.msg,
        type: 'success',
      })
      identifyCode.value = ''
      identifyCode.value += res.data
      setTime()
    })
    .catch((res) => {
      TimeState.value = true
    })
}
// 计时器
const setIntervalTime = ref(null)
const Timetext = ref('发送验证码')
const setTime = () => {
  let Time = 60
  setIntervalTime.value = setInterval(() => {
    if (Time == 0) {
      clearInterval(setIntervalTime.value)
      Timetext.value = '发送验证码'
      TimeState.value = true
    } else {
      Time--
      Timetext.value = Time + 'S'
    }
  }, 1000)
}

// 点击确认
const submitForm = async (formEl) => {
  loading.value = true
  await formEl.validate((valid, fields) => {
    if (valid) {
      proxy
        .$post('/mt4/Member/change_pay_password', ruleForm)
        .then((res) => {
          ElMessage({
            message: res.msg,
            type: 'success',
          })
          loading.value = false
          formEl.resetFields()
        })
        .catch((res) => {
          loading.value = false
        })
    } else {
      loading.value = false
    }
  })
}
</script>

<style scoped lang="less">
.GetCode {
  padding: 0 20px;
  box-sizing: border-box;
  cursor: pointer;
  background: #212121;
  border: 1px solid #434343;
}

/deep/ .el-input-group__append,
.el-input-group__prepend {
  padding: 0 !important;
}

.refruleForm {
  width: 400px;
  height: 100%;
  margin: 30px 0;

  .btn {
    width: 100%;
    border-radius: 25px;
    border: 1px solid #bb8d62;
    background-color: #dcd5cf !important;
    color: #242424 !important;
  }

  .item {
    width: 100%;
  }
}

.newbody {
  width: 100%;
  height: 100%;
  background-color: transparent;
  padding: 40px;
  box-sizing: border-box;

  .newbody-form {
    width: 400px;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    padding: 20px;
    background: linear-gradient(#212121, #0f0f0f);
    border: 1px solid #4f4f4f;
    border-radius: 10px;
  }

  .title {
    margin-bottom: 10px;
    color: #bb8d62;
    font-size: 24px;
    text-align: center;
  }

  .btn {
    width: 100%;
  }
}

/*  ::v-deep */
/deep/ .el-select .el-input__inner {
  width: 100px;
  text-align: center;
}

/deep/ .el-select .el-input__wrapper {
  box-shadow: none !important;
  background-color: transparent !important;
  text-align: center;
}

/deep/.el-input .el-input__wrapper.is-focus {
  box-shadow: none !important;
}

/deep/ .el-select .el-input.is-focus .el-input__wrapper {
  border-color: #dcdfe6 !important;
  box-shadow: none !important;
}

::v-deep {
  .el-input.is-disabled .el-input__wrapper {
    background: #212121;
    border: 1px solid #434343;
    border-radius: 4px;
    box-shadow: none !important;
  }
  .el-input__wrapper {
    background: #212121;
    border: 1px solid #434343;
    border-radius: 4px;
    box-shadow: none !important;

    &:hover {
      box-shadow: none !important;
    }
  }

  .el-input {
    border: 1px solid #434343;
    border-radius: 25px;
  }

  .el-input__inner {
    color: #dcd5cf;
  }
}
</style>
